#include "SDL_Init.h"


SDL_Surface *load_image(string filename, SDL_Color* key)
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

void draw_tile(SDL_Surface* screen, SDL_Surface* source, SDL_Rect* box)
{
	SDL_Rect temp = {0,0, box->w, box->h};

	SDL_BlitSurface(source, &temp, screen, box);
}

SDL_Surface* init(SDL_Surface* screen, int SCREEN_WIDTH, int SCREEN_HEIGHT, int SCREEN_BPP)
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return NULL;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return NULL;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Jeopardy", NULL );

    //If everything initialized fine
    return screen;
}

bool load_files()
{
    //Load the button sprite sheet
    //buttonSheet = load_image( "button.png" );

    //If there was a problem in loading the button sprite sheet
    //if( buttonSheet == NULL )
    //{
    //    return false;
    //}

    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the surface
    SDL_FreeSurface(NULL);

    //Quit SDL
    SDL_Quit();
}